package zhongbo.java.air.flightpath.service.Impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.exception.ExceptionCode;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.flightpath.listener.RedisLogChanelPublisher;
import zhongbo.java.air.flightpath.mapper.FlightPathMapper;
import zhongbo.java.air.flightpath.model.FlightPath;
import zhongbo.java.air.flightpath.service.IFlightPathService;

import java.util.Date;
import java.util.UUID;

@Service
public class FlightPathServiceImpl implements IFlightPathService {

    @Autowired
    FlightPathMapper flightPathMapper;
    @Autowired
    RedisLogChanelPublisher redisLogChanelPublisher;
    @Override
    public int addFlight(FlightPath flightPath) {
        flightPath.setId(null);
        if (flightPath == null || flightPath.equals("")){
            throw new BusinessException(ExceptionCode.ERROR);
        }
            int add = flightPathMapper.insert(flightPath);
            redisLogChanelPublisher.publishMessage(String.format("新增航班路径为：%s",flightPath));
            return add;
    }

    @Override
    public void deleteFlight(String id) {
        if (id == null || id.equals("")){
            throw new BusinessException(ExceptionCode.ERROR);
        }
        FlightPath flightPath = flightPathMapper.selectById(id);
        flightPathMapper.deleteById(id);
        redisLogChanelPublisher.publishMessage(String.format("删除航班路径为：Id=%s,详细详细=%s",id,flightPath));
    }

    @Override
    public int updateFlight(FlightPath flightPath) {
       if (flightPath ==null ||flightPath.equals("")){
           throw new BusinessException(ExceptionCode.ERROR);
       }
       int update = flightPathMapper.updateById(flightPath);
        redisLogChanelPublisher.publishMessage(String.format("更新路径为：%s",flightPath));
        return update;
    }

    @Override
    public FlightPath selectById(String id) {
        if (id == null || id.equals("")){
            throw new BusinessException(ExceptionCode.ERROR);
        }
        FlightPath flightPath = flightPathMapper.selectById(id);
        redisLogChanelPublisher.publishMessage(String.format("查询航班路径: ID=%s, 结果=%s",
                id, flightPath != null ? "找到记录" : "未找到记录"));
        return flightPath;
    }

    @Override
    public PageVo page(String icaoCode, Integer pageNumber, Integer pageSize) {
        QueryWrapper<FlightPath> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(icaoCode)){
            queryWrapper.like("icao_code",icaoCode);
        }
        IPage<FlightPath> pageParam = new Page<>(pageNumber,pageSize);
        IPage<FlightPath> PathPage = flightPathMapper.selectPage(pageParam,queryWrapper);
        redisLogChanelPublisher.publishMessage(String.format("分页查询: 条件是%s, 页码=%d, 大小=%d",
                icaoCode != null ? "icaoCode=" + icaoCode : "无", pageNumber, pageSize));
        return new PageVo<>(PathPage);
    }

    @Override
    public PageVo pageTime(String startTime, String endTime, Integer pageNumber, Integer pageSize) {
        QueryWrapper<FlightPath> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(startTime) || StrUtil.isNotBlank(endTime)){
                queryWrapper.between("timestamp",startTime,endTime);
        }
        IPage<FlightPath> pageParam = new Page<>(pageNumber,pageSize);
        IPage<FlightPath> PathPage = flightPathMapper.selectPage(pageParam,queryWrapper);
        redisLogChanelPublisher.publishMessage(String.format("分页查询: 条件是%s, 页码=%d, 大小=%d",
                startTime != null ? "timestamp=" + startTime : "无", pageNumber, pageSize));
        return new PageVo<>(PathPage);
    }

}
